#include "stdafx.h"
#include <strsafe.h>
#include "ffFileSink.h"



CFfFileSink::CFfFileSink()	
:	m_pszFileName(NULL)
,	m_pszfileA(NULL)
{	
}

CFfFileSink::~CFfFileSink()
{	
	DELETE_POINTER(m_pszfileA);
	DELETE_POINTER(m_pszFileName);
}

STDMETHODIMP_(ULONG) CFfFileSink::AddRef()
{
	return 2;
}

STDMETHODIMP_(ULONG) CFfFileSink::Release()
{
	return 1;
}

STDMETHODIMP CFfFileSink::QueryInterface(REFIID riid, void **ppvObject)
{
	*ppvObject = 0;
	if(riid == IID_IUnknown){
		*ppvObject = (IUnknown*)this;
		return S_OK;
	}else if(riid == IID_IFileSinkFilter){
		*ppvObject = (IFileSinkFilter*)this;
		return S_OK;
	}else{
		return E_FAIL;
	}
}


STDMETHODIMP CFfFileSink::SetFileName(LPCOLESTR pszFileName,const AM_MEDIA_TYPE *pmt)
{	
	CheckPointer(pszFileName,E_POINTER);
	if(m_pszFileName) {
		delete m_pszFileName;
		m_pszFileName = NULL;
	}
	if(m_pszfileA){
		delete m_pszfileA;
		m_pszfileA = NULL;
	}

	size_t path_len;
	HRESULT hr = StringCchLengthW(pszFileName, MAX_PATH, &path_len);
	if(FAILED(hr)) return hr;
	m_pszFileName = new wchar_t[path_len + 1];
	if(!m_pszFileName) return E_OUTOFMEMORY;	
	hr = StringCchCopyW(m_pszFileName, path_len+1, pszFileName);	
	if(FAILED(hr)) return hr;

	m_pszfileA = new char[path_len + 1];
	if(!m_pszfileA) return E_OUTOFMEMORY;
	WideCharToMultiByte(CP_ACP, 0, m_pszFileName, -1, m_pszfileA, (int)path_len, 0, 0);
	m_pszfileA[path_len] = '\0';
	return S_OK;
	


} 


STDMETHODIMP CFfFileSink::GetCurFile(LPOLESTR * ppszFileName,AM_MEDIA_TYPE *pmt)
{
	CheckPointer(ppszFileName, E_POINTER);	
	if(!m_pszFileName) return E_FAIL;
	size_t len;
	HRESULT hr = StringCchLengthW(m_pszFileName, MAX_PATH, &len);
	if(FAILED(hr)) return hr;
	*ppszFileName = (LPOLESTR)QzTaskMemAlloc(sizeof(WCHAR) * (len + 1));	
	if (*ppszFileName == NULL) return E_OUTOFMEMORY; 
	hr = StringCchCopyW(*ppszFileName, len+1, m_pszFileName);
	if(FAILED(hr)) return E_FAIL;

	if(pmt) 
	{
		ZeroMemory(pmt, sizeof(*pmt));
		pmt->majortype = MEDIATYPE_NULL;
		pmt->subtype = MEDIASUBTYPE_NULL;
	}
	return S_OK;
} 

char* CFfFileSink::GetCurFile()
{
	return m_pszfileA;
}

